package indi.kider.server.employee.service;

import indi.kider.common.constant.AppUserType;
import indi.kider.common.model.employee.Worker;
import indi.kider.server.employee.mapper.WorkerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;

/**
 * Created by sq on 2018/6/4.
 */
@Slf4j
@Service
public class WorkerServiceImpl implements WorkerService {
    @Autowired
    private WorkerMapper workerMapper;
    @Autowired
    private UserService userService;
    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @Override
    public Worker getWorkerById(Long id) {
        return workerMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取分页查询
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<List<Worker>> getWorkerPages(PageRequest<Worker> request) {
        PageUtil.start(request);
        return PageUtil.getPageResponse(workerMapper.getWorkerList(request.getQuery()));
    }

    /**
     * 不分页查询
     *
     * @param Worker
     * @return
     */
    @Override
    public List<Worker> getWorkers(Worker Worker) {
        return workerMapper.select(Worker);
    }

    /**
     * 新增
     *
     * @param worker
     * @return
     */
    @Override
    public Worker add(Worker worker) throws Exception {
        if (worker != null) {
            if (worker.getId() == null || worker.getId().equals(0L)) {
                //存在的手机号=======
                String phone = worker.getPhone();
                Worker temp = new Worker();
                temp.setPhone(phone);
                int count = workerMapper.selectCount(temp);
                if(count == 0 ){
                    worker.setId(null);
                    worker.setPwd("123456");
                    workerMapper.insertSelective(worker);
//                    userService.insert(phone, AppUserType.WORKER);
                }else{
                    throw new Exception("手机号码"+phone+"已被注册!");
                }
            }
        }
        return worker;
    }

    /**
     * 更新
     *
     * @param worker
     * @return
     */
    @Override
    public Worker modify(Worker worker) throws Exception{
        if (worker != null && worker.getId() != null && worker.getId() > 0L){
            String phone = worker.getPhone();
            Worker temp = new Worker();
            temp.setPhone(phone);
            int count = workerMapper.selectCount(temp);
            if(count == 0 ){
                workerMapper.updateByPrimaryKeySelective(worker);
            }else if(count == 1 ){
                Worker update = workerMapper.selectOne(temp);
                if(update.getId() == worker.getId()){
                    workerMapper.updateByPrimaryKeySelective(worker);
                }else{
                    throw new Exception("手机号码"+phone+"已被注册!");
                }
            }
        }
        return worker;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delById(Long id) {
        workerMapper.deleteByPrimaryKey(id);
    }
}
